package com.yinhe.music.yhmusic.radio.presenter;

import com.hwangjr.rxbus.RxBus;
import com.yinhe.music.common.utils.JsonUtil;
import com.yinhe.music.event.RxBusEventType;
import com.yinhe.music.type.person.BaseInfo;
import com.yinhe.music.yhmusic.base.BaseModel;
import com.yinhe.music.yhmusic.base.BasePresenter;
import com.yinhe.music.yhmusic.constants.IConstants;
import com.yinhe.music.yhmusic.http.StatusCode;
import com.yinhe.music.yhmusic.network.response.Response;
import com.yinhe.music.yhmusic.network.response.ResponseTransformer;
import com.yinhe.music.yhmusic.radio.RadioActivity;
import com.yinhe.music.yhmusic.radio.contract.IRadioContract;

import io.reactivex.disposables.Disposable;

/**
 * {@link RadioActivity} P层
 *
 * @author 何晓华
 */
public class RadioPresenter extends BasePresenter<BaseModel, IRadioContract.IRadioView> implements IRadioContract.IRadioPresenter {
    @Override
    public void getRadioInfo(int id) {
        if (getView() == null) {
            return;
        }

        Disposable collectDisposable = mModel.getRadioInfo(id)
                .compose(ResponseTransformer.handleResult())
                .compose(schedulerProvider.applySchedulers())
                .subscribe(radioListInfo -> {
                    getView().setRadioUI(radioListInfo);
                }, throwable -> {
                    getView().showMessage(throwable);
                });

        mDisposable.add(collectDisposable);
    }

    @Override
    public void reqCollect(int id, boolean collectStatus) {
        if (getView() == null) {
            return;
        }

        getView().showLoading();

        int[] idList = {id};
        String idListString = JsonUtil.getJsonArray(idList);

        if (collectStatus) {
            Disposable collectDisposable = mModel.batchUnCollection(null, idListString, IConstants.RADIO)
                    .compose(schedulerProvider.applySchedulers())
                    .subscribe(baseInfoResponse -> {
                        handleCollectResp(baseInfoResponse, collectStatus, id);
                    }, throwable -> {
                        handleFail(throwable);
                    });

            mDisposable.add(collectDisposable);
        } else {
            Disposable collectDisposable = mModel.batchCollection(null, idListString, IConstants.RADIO)
                    .compose(schedulerProvider.applySchedulers())
                    .subscribe(baseInfoResponse -> handleCollectResp(baseInfoResponse, collectStatus, id),
                            this::handleFail);

            mDisposable.add(collectDisposable);
        }


    }

    private void handleCollectResp(Response<BaseInfo> baseInfoResponse, boolean collectStatus, int id) {
        if (baseInfoResponse.getCode() == StatusCode.CHANGE_DATA) {
            getView().updateCollectUI(!collectStatus);
            getView().showToast(baseInfoResponse.getMsg());
            RxBus.get().post(RxBusEventType.Radio.COLLECT_RADIO, String.valueOf(id));
        } else {
            getView().showToast(baseInfoResponse.getMsg());
        }
        getView().hideLoading();
    }

    private void handleFail(Throwable throwable) {
        getView().hideLoading();
        getView().showMessage(throwable);
    }


}
